// pages/matching/matching.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalpages: 100,//总页码
    page: 1,//当前页码
    loading: true,
    drivers: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: options
    })
    if (options.from == 'index') {
      wx.setNavigationBarTitle({
        title: '司机推荐'
      })
    }
    that.getData();
  },
  getData: function (res) {
    var that = this;
    if (that.data.page <= that.data.totalpages) {
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api + '/wx_log_test/driver/match',
          data: {
            token: session,
            cargoid: that.data.options.id,
            page: 1,
            perpage: 10
          },
          success: function (res) {
            console.log(res);
            var res = res.data;
            if (res.msg == "OK") {
              var data = res.data;
              if (data.content.length == 0) {
                that.setData({
                  loading: false,
                  loadtips: '暂无相关数据',
                  drivers: []
                })
              } else {
                that.setData({
                  loading: false,
                  drivers: that.data.drivers.concat(res.data.content),
                  totalpages: res.data.totalPages
                })
                if (that.data.page == that.data.totalpages) {
                  that.setData({
                    page: ++that.data.page,
                    loadtips: '暂无更多数据'
                  })
                }
              }
            } else {
              that.setData({
                loading: false,
                loadtips: res.msg
              })
            }
          },
          fail: function (res) {
            that.setData({
              loading: false,
              loadtips: '接口请求失败'
            })
          }
        })
      })
    }

  },
  lower: function (res) {
    var that = this;
    that.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})